package com.rocksea.app.db;

import android.content.Context;
import android.database.Cursor;
import android.os.Environment;


import android.database.sqlite.SQLiteDatabase;
import com.litesuits.orm.LiteOrm;
import com.litesuits.orm.db.DataBaseConfig;
import com.litesuits.orm.db.assit.QueryBuilder;
import com.litesuits.orm.db.assit.SQLiteHelper;
import com.litesuits.orm.db.assit.WhereBuilder;
import com.litesuits.orm.db.model.ColumnsValue;
import com.litesuits.orm.db.model.ConflictAlgorithm;

import java.util.ArrayList;

/**
 * @author by Mrw_v @date on 2018/9/28
 */

public class UploadingDbHelper implements SQLiteHelper.OnUpdateListener
{
    private static LiteOrm liteOrm;
    public static void init(Context context)
    {
        DataBaseConfig config = new DataBaseConfig(context);
        config.dbName = Environment.getExternalStorageDirectory().getPath()+"/RailDatabase/db/UpLoadingTable.db";
        config.dbVersion = 1;
        liteOrm = LiteOrm.newSingleInstance(config);
        //liteOrm.setDebugged(true);
        UpLoadingTable demo = new UpLoadingTable();
        insert(demo);
        liteOrm.delete(demo);
    }

    public static int insert(UpLoadingTable upLoadingTable)
    {
        if (queryCount(upLoadingTable.baseInfoId,upLoadingTable.mode,upLoadingTable.uuid) == 0)
        {
            liteOrm.insert(upLoadingTable);
            return 0;
        }
        return -1;
    }

    public static long queryCount()
    {
        QueryBuilder<UpLoadingTable> queryBuilder = new QueryBuilder<>(UpLoadingTable.class);
        //if (liteOrm != null)
        try
        {
            long count = liteOrm.queryCount(queryBuilder);
            return count;
        }catch (Exception e)
        {
            e.printStackTrace();
            return -1;
        }
        // 0;
    }

    public static ArrayList<UpLoadingTable> queryTable()
    {
        return liteOrm.query(UpLoadingTable.class);
    }

    @Override
    public void onUpdate(SQLiteDatabase db, int oldVersion, int newVersion)
    {

    }

    public static void delete(String baseInfoId,int mode,String uuid)
    {
        WhereBuilder whereBuilder = new WhereBuilder(UpLoadingTable.class);
        whereBuilder.andEquals("baseInfoId",baseInfoId);
        whereBuilder.andEquals("mode",String.valueOf(mode));
        whereBuilder.andEquals("uuid",uuid);
        liteOrm.delete(whereBuilder);
    }

    private static int queryCount(String baseInfoId,int mode,String uuid)
    {
        QueryBuilder<UpLoadingTable> queryBuilder = new QueryBuilder<>(UpLoadingTable.class);
        WhereBuilder whereBuilder = new WhereBuilder(UpLoadingTable.class);
        whereBuilder.andEquals("baseInfoId",baseInfoId);
        whereBuilder.andEquals("mode",String.valueOf(mode));
        whereBuilder.andEquals("uuid",uuid);
        queryBuilder.where(whereBuilder);
        return (int) liteOrm.queryCount(queryBuilder);
    }


    public static void makeUnloading(String baseInfoId,int mode,String uuid)
    {
        WhereBuilder whereBuilder = new WhereBuilder(UpLoadingTable.class);
        whereBuilder.andEquals("baseInfoId",baseInfoId);
        whereBuilder.andEquals("mode",String.valueOf(mode));
        whereBuilder.andEquals("uuid",uuid);
        ColumnsValue columnsValue = new ColumnsValue(new String[]{"isUploading"},new String[]{"1"});
        liteOrm.update(whereBuilder,columnsValue, ConflictAlgorithm.Fail);
    }

    public static UpLoadingTable queryFirstTable()
    {
        String sql = "SELECT *FROM UpLoadingTable LIMIT 1";
        try
        {
            Cursor cursor = liteOrm.getWritableDatabase().rawQuery(sql,new String[]{});
            if (cursor.moveToFirst())
            {
                UpLoadingTable upLoadingTable = new UpLoadingTable();
                upLoadingTable.baseInfoId = cursor.getString(cursor.getColumnIndex("baseInfoId"));
                upLoadingTable.planNum = cursor.getString(cursor.getColumnIndex("planNum"));
                upLoadingTable.pileNo = cursor.getString(cursor.getColumnIndex("pileNo"));
                upLoadingTable.mode = cursor.getInt(cursor.getColumnIndex("mode"));
                upLoadingTable.isUploading = cursor.getInt(cursor.getColumnIndex("isUploading"));
                upLoadingTable.uuid = cursor.getString(cursor.getColumnIndex("uuid"));
                cursor.close();
                return upLoadingTable;
            }
            else
            {
                cursor.close();
                return null;
            }
        }
        catch (Exception e)
        {
            return null;
        }
    }
}
